package bb.lanxing.lib.devices.bryton.bbcp;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@SuppressLint("MissingPermission")
public class BBDeviceAdapter {
    private static final String TAG = "BBDeviceAdapter";
    private String mAddress;
    private BluetoothAdapter mBluetoothAdapter;
    ScanDeviceCallbacks mCallbacks;
    private Context mContext;
    private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt bluetoothGatt, int i, int i2) {
            if (i2 == 2) {
                bluetoothGatt.discoverServices();
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt bluetoothGatt, int i) {
            if (i == 0) {
                if (BBDeviceAdapter.this.mAddress != null || bluetoothGatt.getDevice().getName().equals("DfuTarg")) {
                    BBDeviceAdapter.this.sendUpdateBroadcast(bluetoothGatt.getDevice().getAddress(), bluetoothGatt.getDevice().getName(), BBDeviceAdapter.this.mRssi, BBDeviceAdapter.this.mScanRecord);
                } else if (bluetoothGatt.getService(BbcpManager.BBCP_SERVICE_UUID) != null) {
                    BBDeviceAdapter.this.sendUpdateBroadcast(bluetoothGatt.getDevice().getAddress(), bluetoothGatt.getDevice().getName(), BBDeviceAdapter.this.mRssi, BBDeviceAdapter.this.mScanRecord);
                }
            } else {
                Log.e(BBDeviceAdapter.TAG, "Service discovery error: " + i);
            }
            bluetoothGatt.disconnect();
            bluetoothGatt.close();
        }
    };
    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
        @Override
        public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bArr) {
            if (BBDeviceAdapter.this.mAddress == null || bluetoothDevice.getAddress().equals(BBDeviceAdapter.this.mAddress)) {
                if (BBDeviceAdapter.this.mAddress == null) {
                    BBDeviceAdapter.this.parseUUIDs(bArr);
                    String name = bluetoothDevice.getName();
                    if (name == null) {
                        name = BBDeviceAdapter.this.parseLocalName(bArr);
                    }
                    BBDeviceAdapter.this.sendUpdateBroadcast(bluetoothDevice.getAddress(), name, i, bArr);
                    return;
                }
                BBDeviceAdapter bBDeviceAdapter = BBDeviceAdapter.this;
                bBDeviceAdapter.sendUpdateBroadcast(bBDeviceAdapter.mAddress, bluetoothDevice.getName(), i, bArr);
                BBDeviceAdapter.this.mBluetoothAdapter.stopLeScan(BBDeviceAdapter.this.mLeScanCallback);
            }
        }
    };
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("com.brytonsport.bbcp.BBDEVICE_BROADCAST_SCAN".equals(intent.getAction())) {
                String stringExtra = intent.getStringExtra("address");
                String stringExtra2 = intent.getStringExtra("name");
                int intExtra = intent.getIntExtra("rssi", 0);
                byte[] byteArrayExtra = intent.getByteArrayExtra("scanRecord");
                if (BBDeviceAdapter.this.mCallbacks == null) {
                    return;
                }
                BBDeviceAdapter.this.mCallbacks.onScanDevice(stringExtra, stringExtra2, intExtra, byteArrayExtra);
            }
        }
    };
    private int mRssi;
    private byte[] mScanRecord;
    private boolean mScanning;

    public interface ScanDeviceCallbacks {
        void onScanDevice(String str, String str2, int i, byte[] bArr);
    }

    private boolean isBLEEnabled() {
        return false;
    }

    public BBDeviceAdapter(BluetoothAdapter bluetoothAdapter, Context context) {
        this.mContext = context;
        this.mBluetoothAdapter = bluetoothAdapter;
    }

    public void startScanDevice(ScanDeviceCallbacks scanDeviceCallbacks) {
        if (this.mScanning) {
            return;
        }
        startScanDeviceWithAddress(new UUID[]{BbcpManager.BBCP_SERVICE_UUID, BbcpManager.BBCP_DATA_SERVICE_UUID}, scanDeviceCallbacks, null);
    }

    public void startScanDeviceWithAddress(UUID[] uuidArr, ScanDeviceCallbacks scanDeviceCallbacks, String str) {
        if (this.mScanning) {
            return;
        }
        this.mAddress = str;
        LocalBroadcastManager.getInstance(this.mContext).registerReceiver(this.mReceiver, makeIntentFilter());
        this.mCallbacks = scanDeviceCallbacks;
        this.mScanning = true;
        this.mBluetoothAdapter.startLeScan(this.mLeScanCallback);
    }

    public void stopScanDevice() {
        if (this.mScanning) {
            LocalBroadcastManager.getInstance(this.mContext).unregisterReceiver(this.mReceiver);
            this.mScanning = false;
            this.mBluetoothAdapter.stopLeScan(this.mLeScanCallback);
        }
    }

    private IntentFilter makeIntentFilter() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.brytonsport.bbcp.BBDEVICE_BROADCAST_SCAN");
        return intentFilter;
    }

    public void sendUpdateBroadcast(String str, String str2, int i, byte[] bArr) {
        Intent intent = new Intent("com.brytonsport.bbcp.BBDEVICE_BROADCAST_SCAN");
        intent.putExtra("address", str);
        intent.putExtra("name", str2);
        intent.putExtra("rssi", i);
        intent.putExtra("scanRecord", bArr);
        LocalBroadcastManager.getInstance(this.mContext).sendBroadcast(intent);
    }

    public String parseLocalName(byte[] bArr) {
        int i = 0;
        while (i < bArr.length - 2) {
            int i2 = i + 1;
            int i3 = bArr[i];
            if (i3 == 0) {
                return null;
            }
            int i4 = i2 + 1;
            if (bArr[i2] == 9) {
                StringBuilder sb = new StringBuilder();
                while (i3 > 1) {
                    sb.append((char) bArr[i4]);
                    i3--;
                    i4++;
                }
                return sb.toString();
            }
            i = (i3 - 1) + i4;
        }
        return null;
    }

    public List<UUID> parseUUIDs(byte[] bArr) {
        int i;
        ArrayList arrayList = new ArrayList();
        for (int i2 = 0; i2 < bArr.length - 2; i2 = i) {
            int i3 = i2 + 1;
            int i4 = bArr[i2];
            if (i4 == 0) {
                break;
            }
            i = i3 + 1;
            char c = (char) bArr[i3];
            if (c == 2 || c == 3) {
                while (i4 > 1) {
                    int i5 = i + 1;
                    i4 -= 2;
                    arrayList.add(UUID.fromString(String.format("454D%04x-A122-058D-9B42-9D0F3772AF82", bArr[i] + (bArr[i5] << 8))));
                    i = i5 + 1;
                }
            } else if (c == 6 || c == 7) {
                while (i4 >= 16) {
                    int i6 = i + 1;
                    try {
                        ByteBuffer order = ByteBuffer.wrap(bArr, i, 16).order(ByteOrder.LITTLE_ENDIAN);
                        arrayList.add(new UUID(order.getLong(), order.getLong()));
                    } catch (IndexOutOfBoundsException e) {
                        Log.e(TAG, e.toString());
                    }
                    i = i6 + 15;
                    i4 -= 16;
                }
            } else {
                i += i4 - 1;
            }
        }
        return arrayList;
    }
}
